package myProject.gwt.client;

import java.util.List;

import myProject.gwt.client.dynamicTable.DynamicTable;
import myProject.gwt.client.simpleTable.Header;
import myProject.gwt.client.simpleTable.HeaderCell;
import myProject.gwt.client.simpleTable.StaticTable;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.Event.NativePreviewHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.xml.client.Node;

import cz.sw.gwt.widget.dialog.client.ConfirmDialog;
import cz.sw.gwt.widget.dialog.client.InformationDialogBox;
import cz.sw.gwt.widget.dialog.client.MessageDialog;
import cz.sw.gwt.widget.dialog.client.ConfirmDialog.IResultListener;
import cz.sw.gwt.widget.edit.client.CaptionCheckBoxEdit;
import cz.sw.gwt.widget.edit.client.CaptionNumberEdit;
import cz.sw.gwt.widget.edit.client.CaptionPasswordTextBox;
import cz.sw.gwt.widget.edit.client.CaptionTextArea;
import cz.sw.gwt.widget.edit.client.CaptionTextEdit;
import cz.sw.gwt.widget.edit.client.GenericCaptionComboBox;
import cz.sw.gwt.widget.layout.client.LayoutBase;
import cz.sw.gwt.widget.layout.client.VerticalLayout;

public class MainApp  implements EntryPoint, ResizeHandler {

	  //private BasePanel basePanel = new BasePanel();
  
	private SimplePanel mainPanel = new SimplePanel();
	private LayoutBase content;
	 private HTML table;
	 private DynamicTable dynamicTable;
	 private CheckBox cbCacheCells = new CheckBox("Cache cells");
	 private CheckBox cbFakeScroll = new CheckBox("Fake scroll bar");
	 private CheckBox cbDrawSummary = new CheckBox("Draw summary");
     private TextBox tbRowCount = new TextBox();
     private TextBox tbColsCount = new TextBox();
	 
	  public double derivative(IFunction func, double x){
		  final double dx = 1e-6;
		  return (func.getValue(x + dx) - func.getValue(x)) / dx;
	  }
	  
	  public void onModuleLoad() { 

		  tbColsCount.setText("20");
		  tbRowCount.setText("100");

		  //final HTML style = new HTML("<style type=\"text/css\">.divaa { background-color: red; width:100px; height:100px; display:block;} </style>");
	
		 // el.setInnerText("ahoj");
		  //el.setInnerText("<!--.divaa { background-color: green; width:100px; height:100px; display:block;}-->");
		  //Element el = DOM.createElement("div");
		  //el.setInnerText(".divaa { background-color: red; width:100px; height:100px; display:block;}");
		  //el.setInnerHTML("AAA");
		  //RootPanel.get().getElement().appendChild(el);
		  
		  Button btnCreateTable = new Button("Create dynamic table", new ClickHandler(){

			public void onClick(ClickEvent event) {
				long time = System.currentTimeMillis();
				//createStaticTable();
				createDynamicTable();				
				Window.alert(""+(System.currentTimeMillis() - time));
			}
			  
		  });
		  
		  Button btnCreateStaticTable = new Button("Create static table", new ClickHandler(){

				public void onClick(ClickEvent event) {
					long time = System.currentTimeMillis();
					createStaticTable();			
					Window.alert(""+(System.currentTimeMillis() - time));
				}
				  
			  });

		  Button btnRepaint = new Button("Repaint", new ClickHandler(){

				public void onClick(ClickEvent event) {
					dynamicTable.fullRepaint();
				}
				  
			  });
		  
		  

		  Button resizebutton2 = new Button("Debug", new ClickHandler(){

				public void onClick(ClickEvent event) {
					DebugPanel.setEnableDebug(!DebugPanel.getEnableDebug());
				}
				  
			  });

		  
		  
		  /*
		  HorizontalPanel hp = new HorizontalPanel();
		  hp.add(button);
		  hp.add(resizebutton);
		  hp.add(resizebutton2);
		  
		  String[] columns = new String[20];
		  for(int i=0; i<columns.length; i++)columns[i] = "col"+i;
		  
		  long time = System.currentTimeMillis();
		  StaticTable table = new StaticTable(columns, new VirtualDataSource(10));		  		  
		  
		  //RootPanel.get().add(hp);
		  //RootPanel.get().add(header);
		  //RootPanel.get().add(createHeader());
		  RootPanel.get().add(table);
		  table.init();
		  Window.alert(""+(System.currentTimeMillis() - time));
		  //RootPanel.get().add(mainPanel);
		  
		  RootPanel.get().add(DebugPanel.getInstance());*/
		 // RootPanel.get().add(style);
		//  
		  RootPanel.get().add(btnCreateTable);
		  RootPanel.get().add(btnCreateStaticTable);
		  RootPanel.get().add(btnRepaint);
		  RootPanel.get().add(resizebutton2);
		  RootPanel.get().add(tbRowCount);
		  RootPanel.get().add(tbColsCount);
		  RootPanel.get().add(cbCacheCells);
		  RootPanel.get().add(cbFakeScroll);
		  RootPanel.get().add(cbDrawSummary);
		  RootPanel.get().add(mainPanel);
	  }

	 private int getColCount(){
		 return Integer.parseInt(tbColsCount.getText());
	 }
	 
	 private int getRowCount(){
		 return Integer.parseInt(tbRowCount.getText());
	 }
	  
	private void createDynamicTable(){
		String[] columns = new String[getColCount()];
		for(int i=0; i<columns.length; i++)columns[i] = "col"+i;

		dynamicTable = new DynamicTable(columns, getRowCount());
		dynamicTable.setCacheCells(cbCacheCells.getValue());
		dynamicTable.setFakeScrollBar(cbFakeScroll.getValue());
		dynamicTable.setDrawSummary(cbDrawSummary.getValue());
		mainPanel.setWidget(dynamicTable);
	}
	
	private void createStaticTable(){
		String[] columns = new String[getColCount()];
		for(int i=0; i<columns.length; i++)columns[i] = "col"+i;
		  
  	    StaticTable table = new StaticTable(columns, new VirtualDataSource(getRowCount()));
  	    table.init();
  	    mainPanel.setWidget(table);
	}
	  
	@Override
	public void onResize(ResizeEvent event) {
		//int height = event.getHeight() - 100;
		//content.getContainer().setHeight(height+"px");
	}
	
	private HTML createDivTable(int rowCount, int colCount){
		StringBuilder sb = new StringBuilder();
		sb.append("<div class=\"scroll\"><div class=\"container\">");
		for(int row=0; row<rowCount; row++){						
			for(int col=0; col<colCount; col++){				
				int left = (col % 80)*51;
				int top = row*21;
				String className = "box";
				if((row % 2) == 0) className += " evenRow";
				else className += " oddRow";
				sb.append("<div class=\""+className+"\" style=\"left: "+left+"px; top: "+top+"px;\">A</div>");
			}
		}
		sb.append("</div></div>");
		return new HTML(sb.toString());
	}
	
	private Widget createTable(){
		StringBuilder sb = new StringBuilder();
		sb.append("<table>");
		for(int row=0; row<200; row++){
			sb.append("<tr><div><table><tr>");
			for(int col=0; col<80; col++){				
				sb.append("<td><div><div>col"+col+"</div></div></td>");
			}
			sb.append("</tr></table></div></tr>");
		}
		sb.append("</table>");
		return new HTML(sb.toString());
	}
	
	private void resizeTable(HTML table){
		
		Element el = DOM.getFirstChild(table.getElement());
		el = el.getNextSiblingElement();
		NodeList<Element> list = el.getElementsByTagName("div");
		
		for(int i=0; i<list.getLength(); i++){
			el = list.getItem(i);
			//DOM.setStyleAttribute(el, "width", "200px");
			el.setAttribute("style", "width: 200px;");
		}
		
	}
	
	private void resizeTable2(HTML table, int width){
		
		Element el = DOM.getFirstChild(table.getElement());
		//NodeList<Element> list = el.getElementsByTagName("style");
		el.setInnerText(".col0 {width: "+width+"px;}");
		//Window.alert(el.getTagName());
		
	}
	
	private HTML createVerySimpleTable(){
		StringBuilder sb = new StringBuilder();
		sb.append("<style type=\"text/css\">.myDiv {width: 100px;}</style>");
		sb.append("<table cellpading=\"0\" cellspacing=\"0\">");
		for(int row=0; row<5; row++){
			sb.append("<tr>");
			for(int col=0; col<20; col++){				
				String style;
				
				if((row % 2) == 0) style = "evenRow";
				else style = "oddRow";
				
				
				
				sb.append("<td class=\""+style+"\">");
				
				String divStyle = " col"+col;
				sb.append("<div class=\""+divStyle+"\">col"+col+"</div></td>");
			}
			sb.append("</tr>");
		}
		sb.append("</table>");
		return new HTML(sb.toString());
	}
	
	private Widget createTable2(){
		VerticalPanel vp = new VerticalPanel();
		for(int row=0; row<50; row++){
			HorizontalPanel hp = new HorizontalPanel();
			for(int col=0; col<20; col++){				
				
				hp.add(new Label("col"+col));
			}
			vp.add(hp);
		}
		return vp;
	}
	
	
	
	
}